package com.xyd.Biz.Impl;

import com.xyd.Biz.UserRegBiz;
import com.xyd.Dao.TAddressDao;
import com.xyd.Dao.UserRegDao;
import com.xyd.Entity.TAddress;
import com.xyd.Entity.User;
import com.xyd.Util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

public class UserRegBizImpl implements UserRegBiz {

    UserRegDao userRegDao = null;
    TAddressDao dao = null;

    public UserRegBizImpl() {
        SqlSession session = SqlSessionFactoryUtil.open();
        userRegDao = session.getMapper(UserRegDao.class);
        dao = session.getMapper(TAddressDao.class);
    }

    @Override
    public User UserReg(User user) {

        String add = user.getAddressid();
        user.setAddressid(null);
        int flag1 = userRegDao.UserReg(user);

        TAddress tAddress = new TAddress();
        tAddress.setAddressuserid(user.getUserid());
        tAddress.setAddressdetails(add);
        tAddress.setAddresscategory(0);
        Integer flag2 = dao.addAddressid(tAddress);

        user.setAddressid(Integer.toString(tAddress.getAddressid()));
        Integer flag3 = userRegDao.updateUserAddress(user);

        if (flag1 > 0 && flag2 > 0&& flag3>0) {
            return user;
        } else {
            return null;
        }

    }
}
